package com.dream.keigezhushou.Activity.activity;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RatingBar;
import android.widget.RelativeLayout;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.dream.keigezhushou.Activity.Uiutils.UiUtils;
import com.dream.keigezhushou.Activity.bean.KTVInfo;
import com.dream.keigezhushou.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.dream.keigezhushou.Activity.Uiutils.UiUtils.inflateView;

public class NearKtvActivity extends BaseActivity {

    @BindView(R.id.back)
    ImageView mBack;
    @BindView(R.id.textView)
    TextView mTextView;
    @BindView(R.id.znpx)
    RelativeLayout mZn;
    @BindView(R.id.distance)
    RelativeLayout mDistance;
    @BindView(R.id.price)
    RelativeLayout mPrice;
    @BindView(R.id.list_lv)
    ListView mListLv;
    @BindView(R.id.activity_near_ktv)
    LinearLayout mActivityNearKtv;
//    @BindView(R.id.main_sp1)
//    Spinner mMainSp1;
//    @BindView(R.id.main_sp2)
//    Spinner mMainSp2;
//    @BindView(R.id.main_sp3)
//    Spinner mMainSp3;
//    @BindView(R.id.main_sp4)
//    Spinner mMainSp4;
    @BindView(R.id.sort_tv)
    TextView mSortTv;
    @BindView(R.id.tv_distance)
    TextView mTvDistance;
    @BindView(R.id.tv_price)
    TextView mTvPrice;
    @BindView(R.id.filtrate_tv)
    TextView mFiltrateTv;
    @BindView(R.id.ll)
    LinearLayout mLl;
    @BindView(R.id.sx)
    RelativeLayout mSx;

    private ArrayList<KTVInfo> data = new ArrayList<>();

    private List<String> data_list1;
    private List<String> data_list2;
    private List<String> data_list3;
    private List<String> data_list4;

    private BaseAdapter arr_adapter;
    private ArrayList<Map<String, String>> mMenuData;
    private ArrayList<Map<String, String>> mMenuData2;
    private ArrayList<Map<String, String>> mMenuData3;
    private ArrayList<Map<String, String>> mMenuData4;
    private PopupWindow mPopMenu;

    private ListView mPopListView;
    private SimpleAdapter mMenuAdapter1;
    private SimpleAdapter mMenuAdapter2;
    private SimpleAdapter mMenuAdapter3;
    private SimpleAdapter mMenuAdapter4;
    private int mMenuIndex;
    private String currentProduct, currentSort, currentActivity;
    private String[] mMenuStr;
    private String[] mMenuStr2;
    private String[] mMenuStr3;
    private String[] mMenuStr4;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_near_ktv);
        ButterKnife.bind(this);
        initData();

        initMenuData();
        initPopMenu();
//        initAdapter();
//        setListener();


        mListLv.setAdapter(new MyAdapter(data));
        mListLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                UiUtils.toast(String.valueOf(data.size()));

                UiUtils.toast("跳到KTV详情页");
                Intent intent = new Intent(UiUtils.getContext(), KtvDetailsActivity.class);//Ktv详情页面
//                intent.putExtra("position", position);
//             //   intent.putExtra("url", url);
                startActivity(intent);
            }
        });


    }

    private void initPopMenu() {
        initMenuData();
        View contentView = View.inflate(this, R.layout.popupwindow,
                null);
        mPopMenu = new PopupWindow(contentView,
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        mPopMenu.setOutsideTouchable(true);
        //  mPopMenu.setBackgroundDrawable(new BitmapDrawable());
        ColorDrawable dw = new ColorDrawable(getResources().getColor(R.color.menu_bg_press));
        mPopMenu.setBackgroundDrawable(dw);
       // mPopMenu.setBackgroundDrawable(new ColorDrawable(0x00000000));
        mPopMenu.setFocusable(true);
        // mPopMenu.setAnimationStyle(R.style.popwin_anim_style);
        mPopMenu.setOnDismissListener(new PopupWindow.OnDismissListener() {
            public void onDismiss() {
                mSortTv.setTextColor(Color.parseColor("#333333"));
                mTvDistance.setTextColor(Color.parseColor("#333333"));
                mTvPrice.setTextColor(Color.parseColor("#333333"));
                mFiltrateTv.setTextColor(Color.parseColor("#333333"));
            }
        });

        mPopListView = (ListView) contentView.findViewById(R.id.pops);

        mMenuAdapter1 = new SimpleAdapter(this, mMenuData,
                R.layout.item_listview_popwin, new String[]{"name"},
                new int[]{R.id.listview_popwind_tv});

        mMenuAdapter2 = new SimpleAdapter(this, mMenuData2,
                R.layout.item_listview_popwin, new String[]{"name"},
                new int[]{R.id.listview_popwind_tv});
        mMenuAdapter3 = new SimpleAdapter(this, mMenuData3,
                R.layout.item_listview_popwin, new String[]{"name"},
                new int[]{R.id.listview_popwind_tv});
        mMenuAdapter4 = new SimpleAdapter(this, mMenuData4,
                R.layout.item_listview_popwin, new String[]{"name"},
                new int[]{R.id.listview_popwind_tv});
        mPopListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                mPopMenu.dismiss();
                if (mMenuIndex == 0) {
                    currentProduct = mMenuData.get(position).get("name");

                    mSortTv.setText(currentProduct);

                } else if (mMenuIndex == 1) {
                    currentProduct = mMenuData2.get(position).get("name");

                    mTvDistance.setText(currentProduct);

                } else if (mMenuIndex == 2) {
                    currentProduct = mMenuData3.get(position).get("name");

                    mTvPrice.setText(currentProduct);

                } else {
                    currentProduct = mMenuData4.get(position).get("name");

                    mFiltrateTv.setText(currentProduct);
                }
            }
        });


    }

//    private void setListener() {
//        mMainSp1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
//            @Override
//            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//                Toast.makeText(getApplicationContext(), "点击了" + data_list1.get(position), Toast.LENGTH_SHORT).show();
//            }
//
//            @Override
//            public void onNothingSelected(AdapterView<?> parent) {
//
//            }
//        });
//
//        mMainSp2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
//            @Override
//            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//                Toast.makeText(getApplicationContext(), "点击了" + data_list2.get(position), Toast.LENGTH_SHORT).show();
//            }
//
//            @Override
//            public void onNothingSelected(AdapterView<?> parent) {
//
//            }
//        });
//
//        mMainSp3.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
//            @Override
//            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//                Toast.makeText(getApplicationContext(), "点击了" + data_list3.get(position), Toast.LENGTH_SHORT).show();
//            }
//
//            @Override
//            public void onNothingSelected(AdapterView<?> parent) {
//
//            }
//        });
//
//        mMainSp4.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
//            @Override
//            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//                Toast.makeText(getApplicationContext(), "点击了" + data_list3.get(position), Toast.LENGTH_SHORT).show();
//            }
//
//            @Override
//            public void onNothingSelected(AdapterView<?> parent) {
//
//            }
//        });
//    }

//    private void initAdapter() {
//
//        //适配器
//        arr_adapter = new MydAdapter(data_list1);
//        mMainSp1.setAdapter(arr_adapter);
//
//        //适配器
//        arr_adapter = new MydAdapter(data_list2);
//        mMainSp2.setAdapter(arr_adapter);
//
//        //适配器
//        arr_adapter = new MydAdapter(data_list3);
//        mMainSp3.setAdapter(arr_adapter);
//
//        //适配器
//        arr_adapter = new MydAdapter(data_list4);
//        mMainSp4.setAdapter(arr_adapter);
//
//
//    }

    private void initData() {
        //数据
        data_list1 = new ArrayList<>();


        data_list1.add("智能排序");
        data_list1.add("好评优先");
        data_list1.add("信用优先");
        data_list1.add("距离优先");


        //数据
        data_list2 = new ArrayList<>();
        data_list2.add("距离");
        data_list2.add("周边优先");
        data_list2.add("距离优先");
        data_list2.add("时间优先");

        //数据
        data_list3 = new ArrayList<>();
        data_list3.add("价格");
        data_list3.add("好评优先");
        data_list3.add("离我最近");
        data_list3.add("价格最低");

        data_list4 = new ArrayList<>();
        data_list4.add("筛选");
        data_list4.add("只看免预约");
        data_list4.add("只看可预订");
        data_list4.add("节假日可用");


        for (int i = 0; i < 6; i++) {
            KTVInfo info = new KTVInfo();
            info.setDistance("0.5km");
            info.setIv(getResources().getDrawable(R.mipmap.icon_iv));
            info.setLocation("未央路");
            info.setTitle("欢唱KTV");
            info.setPrivilege("39元/券，6小时欢唱+小吃饮品套餐");
            info.setOrder_detailsorder_details("开心包6小时68元，乐唱包6小时68元");
            info.setPirce("40");
            info.setPingfen("4");
            data.add(info);
        }


    }


    @OnClick({R.id.back, R.id.znpx, R.id.distance, R.id.price, R.id.sx})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;


            case R.id.znpx:
                mSortTv.setTextColor(Color.parseColor("#2692f0"));
                mPopListView.setAdapter(mMenuAdapter1);
                mPopMenu.showAsDropDown(mZn, 0, mMenuStr.length);
                mMenuIndex = 0;


                break;

            case R.id.distance:
                mTvDistance.setTextColor(Color.parseColor("#2692f0"));
                mPopListView.setAdapter(mMenuAdapter2);
                mPopMenu.showAsDropDown(mDistance, 0, mMenuStr2.length);
                mMenuIndex = 1;

                break;

            case R.id.price:
                mTvPrice.setTextColor(Color.parseColor("#2692f0"));
                mPopListView.setAdapter(mMenuAdapter3);
                mPopMenu.showAsDropDown(mPrice, 0, mMenuStr3.length);
                mMenuIndex = 2;

                break;


            case R.id.sx:
//                mFiltrateTv.setSelected(true);
//                mTvPrice.setSelected(false);
//                mTvDistance.setSelected(false);
//                mSortTv .setSelected(false);
                mFiltrateTv.setTextColor(Color.parseColor("#2692f0"));

                mPopListView.setAdapter(mMenuAdapter4);
                mPopMenu.showAsDropDown(mSx, 0, mMenuStr4.length);

                mMenuIndex = 3;
                break;
        }
    }




    class MyAdapter extends BaseAdapter {


        public MyAdapter(ArrayList<KTVInfo> datalist) {
            data = datalist;
        }

        @Override
        public int getCount() {
            return data.size();
        }

        @Override
        public KTVInfo getItem(int position) {
            return data.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            if (convertView == null) {
                convertView = inflateView(R.layout.item_ktvbuy_fragment);
                holder = new ViewHolder();
                holder.iv = (ImageView) convertView.findViewById(R.id.image_iv);
                holder.title = (TextView) convertView.findViewById(R.id.ktv_title);
                holder.pingfen = (TextView) convertView.findViewById(R.id.pingfen);
                holder.bar = (RatingBar) convertView.findViewById(R.id.ratingBar);
                holder.pirce = (TextView) convertView.findViewById(R.id.price);
                holder.location = (TextView) convertView.findViewById(R.id.location);
                holder.distance = (TextView) convertView.findViewById(R.id.distance);
                holder.order_detailsorder_details = (TextView) convertView.findViewById(R.id.order_details);
                holder.privilege = (TextView) convertView.findViewById(R.id.privilege);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            KTVInfo info = getItem(position);
            holder.iv.setImageDrawable(info.getIv());
            holder.title.setText(info.getTitle());
            holder.pingfen.setText(info.getPingfen());
            holder.bar.setRating(Float.parseFloat(info.getPingfen()));
            holder.pirce.setText(info.getPirce());
            holder.location.setText(info.getLocation());
            holder.distance.setText(info.getDistance());
            holder.order_detailsorder_details.setText(info.getOrder_detailsorder_details());
            holder.privilege.setText(info.getPrivilege());


            return convertView;
        }
    }

    static class ViewHolder {
        ImageView iv;
        TextView title;
        TextView pirce;
        TextView pingfen;
        TextView location;
        TextView distance;
        TextView order_detailsorder_details;
        TextView privilege;
        RatingBar bar;
    }

    //适配器
    class MydAdapter extends BaseAdapter {
        private LayoutInflater mLayoutInflater;
        private List<String> mDataList;

        MydAdapter(List<String> dataList) {
            this.mDataList = dataList;
            mLayoutInflater = LayoutInflater.from(NearKtvActivity.this);
        }

        @Override
        public int getCount() {
            return mDataList.size();
        }

        @Override
        public Object getItem(int position) {
            return mDataList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        //数据不太多，没有使用ViewHolder进行处理。
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            convertView = View.inflate(NearKtvActivity.this, R.layout.aaa, null);
            TextView textView = (TextView) convertView.findViewById(R.id.item_main_tv);
            textView.setText(mDataList.get(position));
            return convertView;
        }
    }


    private void initMenuData() {
        mMenuData = new ArrayList<>();
        mMenuStr = new String[]{"全部", "粮油", "衣服", "图书", "电子产品",
                "酒水饮料", "水果"};
        Map<String, String> map;
        for (int i = 0, len = mMenuStr.length; i < len; ++i) {
            map = new HashMap<String, String>();
            map.put("name", mMenuStr[i]);
            mMenuData.add(map);
        }
        mMenuData2 = new ArrayList<>();
        mMenuStr2 = new String[]{"综合排序", "配送费最低"};
        Map<String, String> map2;
        for (int i = 0, len = mMenuStr2.length; i < len; ++i) {
            map2 = new HashMap<String, String>();
            map2.put("name", mMenuStr[i]);
            mMenuData2.add(map2);
        }
        mMenuData3 = new ArrayList<>();
        mMenuStr3 = new String[]{"优惠活动", "特价活动", "免配送费",
                "可在线支付"};
        Map<String, String> map3;
        for (int i = 0, len = mMenuStr3.length; i < len; ++i) {
            map3 = new HashMap<String, String>();
            map3.put("name", mMenuStr3[i]);
            mMenuData3.add(map3);
        }

        mMenuData4 = new ArrayList<>();
        mMenuStr4 = new String[]{"优惠活动", "特价活动", "免配送费",
                "可在线支付"};
        Map<String, String> map4;
        for (int i = 0, len = mMenuStr4.length; i < len; ++i) {
            map4 = new HashMap<String, String>();
            map4.put("name", mMenuStr3[i]);
            mMenuData4.add(map4);
        }


    }


}
